<template>
  <div class="layout">
    <header class="header">
      <div class="header-left">
        <router-link to="/" class="logo">
          <img src="@/assets/logo.png" alt="Visual协同" />
          <span>Visual 协同</span>
        </router-link>
        <div class="nav-menu">
          <router-link to="/" class="nav-item" exact-active-class="router-link-active" exact
            >主页</router-link
          >
          <router-link to="/upload" class="nav-item" active-class="router-link-active"
            >上传图片</router-link
          >
          <router-link to="/picture_editor" class="nav-item" active-class="router-link-active"
            >创作图片</router-link
          >
          <router-link to="/space" class="nav-item" active-class="router-link-active"
            >创建空间</router-link
          >
          <router-link
            :to="{
              path: '/space_detail',
              query: {
                id: userStore.userInfo.spaceId,
              },
            }"
            class="nav-item"
            active-class="router-link-active"
            >我的空间</router-link
          >
          <router-link to="/ai" class="nav-item" active-class="router-link-active"
            >AI作图</router-link
          >
        </div>
      </div>
      <div class="header-right">
        <el-button
          type="primary"
          class="login-btn"
          @click="$router.push('/login')"
          v-if="!userStore.userInfo.id"
        >
          登录
        </el-button>
        <!-- 用户登录后显示 -->
        <el-dropdown
          class="user-dropdown"
          placement="bottom-end"
          trigger="click"
          popper-class="custom-user-dropdown"
          v-else
        >
          <span class="el-dropdown-link">
            <img class="user-avatar" :src="userStore.userInfo.userAvatar || '@/assets/logo.png'" />
            {{ userStore.userInfo?.userName || '用户' }}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="$router.push('/profile')">个人中心</el-dropdown-item>
              <el-dropdown-item
                v-if="userStore.userInfo.userRole === 'admin'"
                @click="$router.push('/admin')"
                >管理员中心</el-dropdown-item
              >
              <el-dropdown-item divided @click="userStore.logout()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script setup>
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
</script>

<style scoped>
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #1677ff;
  font-weight: bold;
  font-size: 18px;
  margin-right: 40px;
}

.logo img {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.nav-menu {
  display: flex;
  gap: 24px;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 14px;
  padding: 8px 0;
  position: relative;
}

.nav-item:hover,
.nav-item.router-link-active {
  color: #1677ff;
}

.nav-item.router-link-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1677ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.login-btn {
  font-size: 14px;
}

.main-content {
  flex: 1;
  padding: 20px;
  background-color: #f5f7fa;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}
.el-dropdown-link {
  display: flex;
  align-items: center;
  color: #333;
  text-decoration: none;
}
.el-dropdown-link:hover {
  color: #1677ff;
}
.el-dropdown-menu {
  min-width: 150px;
}
.el-dropdown-item {
  cursor: pointer;
  padding: 10px 20px;
  color: #333;
}
.el-dropdown-item:hover {
  background-color: #f5f7fa;
  color: #1677ff;
}
.el-dropdown-item.divided {
  border-top: 1px solid #e4e7ed;
}
.el-icon--right {
  margin-left: 5px;
}
</style>
